<?php
    include('checkAdminAccess.php');
    $iniVars = parse_ini_file('../../config.ini',TRUE);
    include('../classes/adodb5/adodb.inc.php');
    include('../classes/class.upload.php');
    include('../function/db.func.php');
    
    if(isset($_POST['cmapid']))
    {
        $cMapTitle = $_POST['cMapTitle'];
        $sql = "UPDATE contact_map SET cmap_title='$cMapTitle' WHERE cmap_id=".$_POST['cmapid'];
        $result =$db->Execute($sql);
        if($result)
        {
            if($_FILES['cMapImg']['name']!='')
            {
                $handle = new Upload($_FILES['cMapImg']);
                if($handle->image_src_x==580 AND $handle->image_src_y==450)
                {
                    if ($handle->uploaded)
                    {
        
                        $handle->Process($iniVars['Locations']['sitepath'].'images/cmap');
            
                        if ($handle->processed)
                        {
                            $sql = 'UPDATE contact_map SET cmap_path="images/cmap/'.$handle->file_dst_name.'" WHERE cmap_id='.$_POST['cmapid'];
                            $result =$db->Execute($sql);
                            if($result)
                            {
                                echo <<<js
                                <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
                                <script type="text/javascript">
                                    $(".submit_indicator", top.document).css('display','none');
                                    alert('แก้ไขข้อมูลเรียบร้อยแล้ว');
                                    top.location='../index.php?page=contact';
                                </script>
js;
                            }
                            else
                            {
                                unlink($iniVars['Locations']['sitepath'].'images/cmap/'.$handle->file_dst_name);
                                echo <<<js
                                <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
                                <script type="text/javascript">
                                    $(".submit_indicator", top.document).css('display','none');
                                    alert('ไม่สามารถอัพโหลดรูปแผนที่ได้ กรุณาแก้ไขภายหลัง');
                                    top.location='../index.php?page=contact';
                                </script>
js;
                            }
                        }
                        else
                        {
                            echo <<<js
                            <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
                            <script type="text/javascript">
                                $(".submit_indicator", top.document).css('display','none');
                                alert('ไม่สามารถอัพโหลดรูปแผนที่ได้ กรุณาแก้ไขภายหลัง');
                                top.location='../index.php?page=contact';
                            </script>
js;
                        }
            
                    }
                    else
                    {
                        echo <<<js
                        <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
                        <script type="text/javascript">
                            $(".submit_indicator", top.document).css('display','none');
                            alert('ไม่สามารถอัพโหลดรูปแผนที่ได้ กรุณาแก้ไขภายหลัง');
                            top.location='../index.php?page=contact';
                        </script>
js;
                    }
                }
                else
                {
                    echo <<<js
                    <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
                    <script type="text/javascript">
                        $(".submit_indicator", top.document).css('display','none');
                        alert('รูปแผนที่ขนาดความกว้าง 580x450 pixel เท่านั้น');
                        $("#img_error_msg", top.document).css('display','block');
                        top.location='../index.php?page=contact';
                    </script>
js;
                }
            }
            else
            {
                echo <<<js
                <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
                <script type="text/javascript">
                    $(".submit_indicator", top.document).css('display','none');
                    alert('แก้ไขข้อมูลเรียบร้อยแล้ว');
                    top.location='../index.php?page=contact';
                </script>
js;
            }
        }
        else
        {
            echo <<<js
            <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
            <script type="text/javascript">
                $(".submit_indicator", top.document).css('display','none');
                alert('ไม่สามารถแก้ไขข้อมูลได้ กรุณาลองใหม่อีกครั้ง');
                top.location='../index.php?page=contact';
            </script>
js;
        }
    }
    else
    {
        $sql = "INSERT INTO contact_map VALUES('','$cMapTitle','')";
        $result =$db->Execute($sql);
        if($result)
        {
            $cmapid = $db->Insert_ID();
            if($_FILES['cMapImg']['name']!='')
            {
                $handle = new Upload($_FILES['cMapImg']);
                if($handle->image_src_x==580 AND $handle->image_src_y==450)
                {
                    if ($handle->uploaded)
                    {
        
                        $handle->Process($iniVars['Locations']['sitepath'].'images/cmap');
            
                        if ($handle->processed)
                        {
                            $sql = 'UPDATE contact_map SET cmap_path="images/cmap/'.$handle->file_dst_name.'" WHERE cmap_id='.$cmapid;
                            $result =$db->Execute($sql);
                            if($result)
                            {
                                echo <<<js
                                <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
                                <script type="text/javascript">
                                    $(".submit_indicator", top.document).css('display','none');
                                    alert('เพิ่มข้อมูลเรียบร้อยแล้ว');
                                    top.location='../index.php?page=contact';
                                </script>
js;
                            }
                            else
                            {
                                unlink($iniVars['Locations']['sitepath'].'images/cmap/'.$handle->file_dst_name);
                                echo <<<js
                                <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
                                <script type="text/javascript">
                                    $(".submit_indicator", top.document).css('display','none');
                                    alert('ไม่สามารถอัพโหลดรูปแผนที่ได้ กรุณาแก้ไขภายหลัง');
                                    top.location='../index.php?page=contact';
                                </script>
js;
                            }
                        }
                        else
                        {
                            echo <<<js
                            <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
                            <script type="text/javascript">
                                $(".submit_indicator", top.document).css('display','none');
                                alert('ไม่สามารถอัพโหลดรูปแผนที่ได้ กรุณาแก้ไขภายหลัง');
                                top.location='../index.php?page=contact';
                            </script>
js;
                        }
            
                    }
                    else
                    {
                        echo <<<js
                        <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
                        <script type="text/javascript">
                            $(".submit_indicator", top.document).css('display','none');
                            alert('ไม่สามารถอัพโหลดรูปแผนที่ได้ กรุณาแก้ไขภายหลัง');
                            top.location='../index.php?page=contact';
                        </script>
js;
                    }
                }
                else
                {
                    echo <<<js
                    <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
                    <script type="text/javascript">
                        $(".submit_indicator", top.document).css('display','none');
                        alert('รูปแผนที่ขนาดความกว้าง 580x450 pixel เท่านั้น');
                        top.location='../index.php?page=contact';
                    </script>
js;
                }
            }
            else
            {
                echo <<<js
                <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
                <script type="text/javascript">
                    $(".submit_indicator", top.document).css('display','none');
                    alert('รูปภาพแผนที่ยังไม่ถูกอัพโหลด กรุณาเพิ่มรูปภาพภายหลัง');
                    top.location='../index.php?page=contact';
                </script>
js;
            }
        }
        else
        {
            echo <<<js
            <script src="{$iniVars['Locations']['siteurl']}js/jquery.js" type="text/javascript"></script>
            <script type="text/javascript">
                $(".submit_indicator", top.document).css('display','none');
                alert('ไม่สามารถเพิ่มข้อมูลได้ กรุณาลองใหม่อีกครั้ง');
                top.location='../index.php?page=contact';
            </script>
js;
        }
    }
?>